
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Web;
using System.Web.UI.WebControls;


using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Gui;
using Swaf.Security;
using UserLogin;
using UserLogin.Gui;
using Syrinx.Gui;

namespace UserManagement.Gui
{
	

	public class UserProfileBizProc : BaseBusinessProcess
	{
		public UserProfileBizProc(ActiveBizProc procState) : base(procState) 
		{
			
		}
		protected IBizObj m_user = null;
				
		protected override object[] getUpdateBizRuleParameters(IBizObj bizObj)
		{
			if (bizObj != null)
				bizObj.put("contact.id", bizObj.get("id"));
			return base.getUpdateBizRuleParameters(bizObj);
		}

		protected override object getCurrentBizObjId()
		{
			return true.Equals(CurrentUser.get("authenticated"))
				? CurrentUser.get("id") as Swaf.BGuid.Guid
				: createNewBizObj().get("id") as Swaf.BGuid.Guid;
		}
		protected override object[] getDetailsBizRuleParameters(object keyValue)
		{
			if (keyValue == null)
				keyValue = true.Equals(CurrentUser.get("authenticated")) 
					? CurrentUser.get("id") as Swaf.BGuid.Guid 
					: createNewBizObj().get("id") as Swaf.BGuid.Guid;
			return new object[] {keyValue};
		}

		public override IResults updateBizObj(IBizObj obj)
		{
			bool isValid = false;
			IResults rc = null;
			List<ValidationError> errors = obj.validate();
			if (errors == null || errors.Count == 0)
				if(string.Compare((string)obj.get("password"), (string)obj.get("password2")) == 0)
					isValid = true;
				else
					m_procState.BPC.Messenger.add("Error-InvalidParameter", new ValidationError("User", "password", "Your password and confirmation password do not match.", ""));
			else
				foreach (ValidationError e in errors)
					m_procState.BPC.Messenger.add("Error-InvalidParameter", e);

			if(isValid)
			{
				rc = base.updateBizObj(obj);
				IBizObj paymentControl = rc.CallResults as IBizObj;
				if (paymentControl != null && !Cmn.IsEmpty(paymentControl.get("redirectUrl")))
				{
				}
				if (rc.ExceptionInfo == null)
				{
					IBizObj wizardInfo = m_procState.get("info") as IBizObj;
					if (wizardInfo != null)
						wizardInfo.put("stepCompleted", true);
				}
			}
			return rc;
		}


		public override IBizObj createNewBizObj()
		{
			IBizObj user = App.bizObjMgr.create("User");
			user.put("id", new Swaf.BGuid.Guid());
			user.put("companyId", ActiveCompanyId);
			return user;
		}
		protected override void setupRuleNames()
		{
			m_getBizObjDetailsRule = "getUser";
			m_updateBizObjRule = "updateUserInfo";
		}
		public override Privileges CurrentEditPrivilege
		{
			get { return true.Equals(CurrentUser.get("authenticated")) ? Privileges.Edit : Privileges.None; }
		}
	
	}
}
